﻿Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports Aricie.DNN.Modules.LuceneSearch.ModuleProviders
Imports Lucene.Net.Documents
Imports DotNetNuke.Entities.Portals
Imports DotNetNuke.Entities.Modules
Imports System.Globalization
Imports DotNetNuke.Services.Search
Namespace Aricie.DNN.Modules.LuceneSearch.ModuleProviders


    Public Class TestProvider
        Implements ILuceneFieldGlossary
        Implements ILuceneStandAloneProgressiveSearchable

        Public Class FileInfo

            Private _fileName As String
            Private _fileSize As String
            Private _fileDate As Date
            Private _filePathWeb As String
            Private _filePathPhys As String

            Public Sub New()
            End Sub

            Public Sub New(ByVal fileName As String)
                _fileName = fileName
            End Sub


            Public Property FileName() As String
                Get
                    Return _fileName
                End Get
                Set(ByVal value As String)
                    _fileName = value
                End Set
            End Property

            Public Property FileSize() As String
                Get
                    Return _fileSize
                End Get
                Set(ByVal value As String)
                    _fileSize = value
                End Set
            End Property

            Public Property FileDate() As Date
                Get
                    Return _fileDate
                End Get
                Set(ByVal value As Date)
                    _fileDate = value
                End Set
            End Property

            Public Property FilePathWeb() As String
                Get
                    Return _filePathWeb
                End Get
                Set(ByVal value As String)
                    _filePathWeb = value
                End Set
            End Property

            Public Property FilePathPhys() As String
                Get
                    Return _filePathPhys
                End Get
                Set(ByVal value As String)
                    _filePathPhys = value
                End Set
            End Property
        End Class


        Public Sub UpgradeSearchItem(portalId As Integer, ByRef searchItem As Aricie.DNN.Modules.LuceneSearch.Business.LuceneSearchItemInfo) Implements Aricie.DNN.Modules.LuceneSearch.ModuleProviders.ILuceneSearchableUpgrade.UpgradeSearchItem
            'Dim pc As New PortalController
            ''Dim sh As New SupplierHelper
            'Dim portal As PortalInfo = pc.GetPortal(portalId)
            'Dim ioFile As New IO.FileInfo(searchItem.SearchKey)
            Dim factory As FieldFactory = FieldFactory.Instance
            'Dim mc As New ModuleController
            'Dim settings As Hashtable = mc.GetModuleSettings(searchItem.ModuleId)


            'Dim file As FileInfo = sh.GetDataSourceFile(ioFile, portal.HomeDirectory, portal.HomeDirectoryMapPath)

            Using contentReader As New System.IO.StreamReader(searchItem.SearchKey)

                Dim line As String = contentReader.ReadLine()

                Do While line IsNot Nothing
                    line = line.Replace("-", "î")
                    line = line.Replace("?", "ï")
                    searchItem.AdditionalFields.Add(factory.CreateField(Fields.FileContent, line))
                    line = contentReader.ReadLine()
                Loop



                searchItem.AdditionalFields.Add(factory.CreateField(Fields.Supplier, searchItem.Description))
                searchItem.AdditionalFields.Add(factory.CreateField(Fields.SupplierPath, "chemin"))
                searchItem.AdditionalFields.Add(factory.CreateField(LuceneGeneralField.FileSize, 10240))
                searchItem.AdditionalFields.Add(factory.CreateField(Fields.FileDate, searchItem.PubDate.ToString(New CultureInfo("en-US"))))
            End Using
        End Sub

        Public Function GetSearchItems(portalId As Integer) As DotNetNuke.Services.Search.SearchItemInfoCollection Implements Aricie.DNN.Modules.LuceneSearch.ModuleProviders.ILuceneStandAloneSearchable.GetSearchItems
            Dim folderPaths = New String() {"", ""}

            Dim toReturn As New SearchItemInfoCollection


            For Each file As String In GetFiles("C:\dev\DNN7\Website\Portals\0\suppliers\suppliers")
                'System.IO.File.
                Dim filename As String = System.IO.Path.GetFileName(file)
                Dim context = "amsv"

                toReturn.Add(New SearchItemInfo(filename, context, -1, DateTime.Now, -1, file, String.Empty, String.Format("Path={0}", file)))
                'If toReturn.Count = 10 Then
                '    Return toReturn
                'End If
            Next




            Return toReturn
        End Function

        Public Function GetFieldDefinitions() As System.Collections.Generic.IList(Of Aricie.DNN.Modules.LuceneSearch.Business.FieldDefinition) Implements ILuceneFieldGlossary.GetFieldDefinitions
            Dim toReturn As New List(Of FieldDefinition)

            toReturn.Add(New FieldDefinition(Fields.FileContent, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO, False, 1))
            toReturn.Add(New FieldDefinition(Fields.FileDate, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO, False, 1))
            toReturn.Add(New FieldDefinition(Fields.SupplierPath, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS, False, 3))
            toReturn.Add(New FieldDefinition(Fields.Supplier, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO, False, 1))

            For Each fd As FieldDefinition In toReturn
                fd.DisableBackingSortableField()
            Next



            Return toReturn
        End Function

        Public Class Fields

            Public Const Supplier As String = "Supplier"
            Public Const SupplierPath As String = "SupplierPath"
            Public Const FileContent As String = "FileContent"
            Public Const FileDate As String = "FileDate"

        End Class

        Private Shared Function GetFiles(path As String) As IEnumerable(Of String)
            Dim result As New List(Of String)
            For Each d As String In System.IO.Directory.GetDirectories(path)
                result.AddRange(GetFiles(d))
            Next
            For Each f As String In System.IO.Directory.GetFiles(path)
                result.Add(f)
            Next
            Return result
        End Function


    End Class
End Namespace